package vns.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import vns.client.service.SupperAdminService;
import vns.server.domain.RuleAction;
import vns.server.domain.ObjectType;
import vns.server.domain.SupperAdmin;
import vns.shared.RuleActionDTO;
import vns.shared.ObjectTypeDTO;
import vns.shared.SupperAdminDTO;

import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class SupperAdminServiceImpl extends RemoteServiceServlet implements
		SupperAdminService {

	@Override
	public SupperAdminDTO Create(SupperAdminDTO supperadminDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		SupperAdmin action = null;
		try {
			action = new SupperAdmin(supperadminDTO);
			pm.makePersistent(action);
		} finally {
			pm.close();
		}
		return action.toDTO();
	}

	@Override
	public boolean Remove(Key id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			SupperAdmin action = pm.getObjectById(SupperAdmin.class, id);
			if (action != null) {
				pm.deletePersistent(pm.getObjectById(SupperAdmin.class, id));
			}
		} finally {
			pm.close();
		}
		return true;
	}

	@Override
	public SupperAdminDTO Loadbyid(Key id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		SupperAdminDTO actionDTO = null;
		try {
			SupperAdmin action = pm.getObjectById(SupperAdmin.class, id);
			actionDTO = action.toDTO();
		} finally {
			pm.close();
		}
		return actionDTO;
	}

	@Override
	public SupperAdminDTO Update(SupperAdminDTO supperadminDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		SupperAdmin action = null;
		try {
			if (supperadminDTO.getId() == null) {
				action = new SupperAdmin(supperadminDTO);
				pm.makePersistent(action);
			} else {
				action = pm.getObjectById(SupperAdmin.class,
						supperadminDTO.getId());
				action.updatefromDTO(supperadminDTO);
			}
		} finally {
			pm.close();
		}

		return action.toDTO();
	}

	@Override
	public ArrayList<SupperAdminDTO> Loadall() {
		ArrayList<SupperAdminDTO> listactionDTO = new ArrayList<SupperAdminDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(SupperAdmin.class);
		try {
			List<SupperAdmin> res = (List<SupperAdmin>) q.execute();
			for (SupperAdmin actiontype : res) {
				listactionDTO.add(actiontype.toDTO());
			}
		} finally {
			pm.close();
		}
		return listactionDTO;
	}

	@Override
	public void Updateall(List<SupperAdminDTO> listNew) {
		// TODO Auto-generated method stub

	}

	@Override
	public String test() {
		BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
		return blobstoreService.createUploadUrl("/upload");
	}

}
